﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;



namespace H2.NET.Web.Pages
{
    public partial class TabScan : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }
                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

         public bool IsHaveAttachment
        {
            get
            {
                return ViewState["IsHaveAttachment"] == null ? false : (bool)ViewState["IsHaveAttachment"];
            }
            set
            {
                ViewState["IsHaveAttachment"] = value;
            }
        }

        //private string[] P_Plant
        //{
        //    get
        //    {
        //        if (StoreAPI["P_Plant~" + Key] == null)
        //        {
        //            IList<m_Plant> dlts = (m_Plant.status == 1 & m_Plant.categary == "P").List<m_Plant>();
        //            string[] p_plant = new string[dlts.Count+1];
        //            if (TaskAPI.Process == "PKG Sefi Code BOM Application")
        //            {
        //                p_plant[0] = "K15P";
        //            }
        //            else
        //            {
        //                p_plant[0] = "";
        //            }
                    
        //            for (int i = 0; i < dlts.Count; i++)
        //            {
        //                p_plant[i+1] = dlts[i].PlantName;
        //            }

        //            StoreAPI["P_Plant~" + Key] = p_plant;
        //        }

        //        return (string[])StoreAPI["P_Plant~" + Key];
        //    }
        //}

       
        

        bool flg_Tech;
        bool flg_pur;
        bool flg_pln;
        bool flg_new;
        bool flg_prd;
        bool flg_mkt;
        bool flg_revision;
        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
            
            #region SCAN
            if (Status.Category == "SCAN")
            {
                bool flg = Status.TradeMarketing == "C" && TaskAPI.IsAllowSubmit;

                gv_Scan.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 20, Align.Left, false, "No", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("01", "调整属性", BoundColumnType2.DropDownList, 70, Align.Left, flg, "Type", InputType.None, new string[3] { "", "Creation", "Revision" }));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("02", "是否需要扫描", BoundColumnType2.DropDownList, 100, Align.Left, flg, "Scan", InputType.None, new string[3] { "", "Yes", "No" }));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("03", "BPCS Code", BoundColumnType2.TextBox, 100, Align.Left, flg, "BPCS_Code", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("04", "产品名称", BoundColumnType2.TextBox, 70, Align.Left, flg, "Production_Name", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("05", "Material（SAP Code）", BoundColumnType2.TextBox, 150, Align.Left, flg, "SAP_Code", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("06", "Valid From 起始日期", BoundColumnType2.Calender, 150, Align.Left, flg, "Valid_From", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("07", "Valid To 截止日期", BoundColumnType2.Calender, 100, Align.Left, flg, "Valid_To", InputType.None, null));
                gv_Scan.DynamicColumn.Add(new BoundColumn2("08", "Remark", BoundColumnType2.TextBox, 150, Align.Left, flg, "Remark", InputType.None, null));
                gv_Scan.Width = 1000;
                SetEnable(flg, gv_Scan_Add);
                SetEnable(flg, gv_Scan_Delete);
                //SetVisible(gv_Plg.Enabled && gv_Plg.Visible, gv_Plg_Add, gv_Plg_Delete);
                if (!Page.IsPostBack)
                {
                    Scan_P15.Visible = true;
                    gv_Scan.Key = Key;

                    #region 发起步骤
                    if (TaskAPI.IsInitiate)
                    {
                        List<mdc_Material_Scan> scan_s = new List<mdc_Material_Scan>();
                        for (int i = 0; i < 10; i++)
                        {
                            scan_s.Add(new mdc_Material_Scan());
                            scan_s[i].No = i + 1;
                        }
                        gv_Scan.DataSource = scan_s;
                    }
                    #endregion

                    #region 审批环节
                    else
                    {
                        IList<mdc_Material_Scan> scan_s = (mdc_Material_Scan.process == TaskAPI.Process & mdc_Material_Scan.incident == TaskAPI.Incident).List<mdc_Material_Scan>();
                        if (scan_s.Count == 0)
                        {
                            for (int i = 0; i < 6; i++)
                            {
                                scan_s.Add(new mdc_Material_Scan());
                                scan_s[i].No = i + 1;
                            }
                        }
                        gv_Scan.DataSource = scan_s;
                    
                    }
                    #endregion

                }
            }
            #endregion

        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Scan_Add":
                    List<mdc_Material_Scan> scan_s = (List<mdc_Material_Scan>)gv_Scan.DataSource;
                    mdc_Material_Scan scan = new mdc_Material_Scan();
                    scan.No = scan_s.Count + 1;
                    scan_s.Insert(gv_Scan.ChangedRowIndex == -1 || gv_Scan.ChangedRowIndex >= scan_s.Count ? scan_s.Count : gv_Scan.ChangedRowIndex, scan);
                    gv_Scan.DataSource = scan_s;
                    gv_Scan.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "gv_Scan_Delete":
                    List<mdc_Material_Scan> scan_s = (List<mdc_Material_Scan>)gv_Scan.DataSource;
                    IList<mdc_Material_Scan> list = (mdc_Material_Scan.process == TaskAPI.Process & mdc_Material_Scan.incident == TaskAPI.Incident).List<mdc_Material_Scan>();
                    if (scan_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    scan_s.RemoveAt(gv_Scan.ChangedRowIndex == -1 || gv_Scan.ChangedRowIndex >= scan_s.Count ? scan_s.Count - 1 : gv_Scan.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Material_Scan scan_1 in list)
                    {
                        if (string.IsNullOrEmpty(scan_1.SAP_Code))
                        {
                            continue;
                        }
                        foreach (mdc_Material_Scan scan_2 in scan_s)
                        {

                            if (string.IsNullOrEmpty(scan_2.SAP_Code))
                            {
                                continue;
                            }
                            if (scan_2.ID == scan_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(scan_1);
                        }
                        flag = false;
                    }

                    gv_Scan.DataSource = scan_s;
                    gv_Scan.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region SCAN
                case "SCAN":
                    foreach (mdc_Material_Scan scan in (List<mdc_Material_Scan>)gv_Scan.DataSource)
                    {
                        //if (string.IsNullOrEmpty(pkg_fini.FINI_Code))
                        //{
                        //    continue;
                        //}
                        scan.Process = TaskAPI.Process;
                        scan.Incident = TaskAPI.Incident;
                        scan.UserName = UserAPI.UserName;
                        scan.Time = System.DateTime.Now;
                        scan.Status = 0;
                        Storage.Save(scan);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        //#region 方法 DataValidation(数据验证)
        //public List<string> DataValidation()
        //{
        //    //如果当前 Tabs 是隐藏的情况，退出
        //    if (!Status.IsEdited)
        //    {
        //        return new List<string>();
        //    }

        //    List<string> vald_s = new List<string>();
        //    List<Regular> regs = new List<Regular>();

        //    if (Status.Category == "MTO-SEFI" && IsHaveAttachment)
        //    {
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Base_Quantity.Text, "&nbsp;&nbsp;Base Quantity 为必填"));
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Plant.Text, "&nbsp;&nbsp;Plant 为必填"));
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_MTO_Code.Text, "&nbsp;&nbsp;MTO Code 为必填"));
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Formula_Code.Text, "&nbsp;&nbsp;Formula Code 为必填"));
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Color_Code.Text, "&nbsp;&nbsp;Color Code 为必填"));
        //        regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Product_KPL.Text, "&nbsp;&nbsp;Product KPL 为必填"));



        //        foreach (mdc_Bom_MTO_Sefi_2 mto_sefi in (List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)
        //        {
        //            if (string.IsNullOrEmpty(mto_sefi.Ict) && string.IsNullOrEmpty(mto_sefi.Routing_Text) && string.IsNullOrEmpty(mto_sefi.Component_SAP_Code))
        //            {
        //                continue;
        //            }

        //            if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "L")
        //            {
        //                mto_sefi.Routing_Text = "";
        //                regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_SAP_Code, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component SAP Code 为必填"));
        //                regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Quantity, "&nbsp;&nbsp;行" + mto_sefi.No + ": Quantity 为必填"));
        //                //regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_Description, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component Description 为必填"));
        //            }

        //            if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "T")
        //            {
        //                mto_sefi.Component_Description = "";
        //                mto_sefi.Component_SAP_Code = "";
        //                regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Routing_Text, "&nbsp;&nbsp;行" + mto_sefi.No + ": Routing Text为必填"));

        //            }
        //        }

        //        //if (string.IsNullOrEmpty(((List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)[0].Ict))
        //        //{
        //        //    regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + "BOM: 明细行中至少填写 1 行数据"));
        //        //}
        //    }


        //    if (Status.Category == "SEFI" && IsHaveAttachment)
        //    {

        //        foreach (mdc_Bom_Sefi_1 SEFI in (List<mdc_Bom_Sefi_1>)gv_SEFI_1.DataSource)
        //        {
        //            if (string.IsNullOrEmpty(SEFI.Material) && string.IsNullOrEmpty(SEFI.Plant) && string.IsNullOrEmpty(SEFI.Alternative_BOM)
        //              && string.IsNullOrEmpty(SEFI.Lot_Size) && string.IsNullOrEmpty(SEFI.Base_Quantity))
        //            {
        //                continue;
        //            }
        //            //赋值
        //            if (SEFI_Material == "") SEFI_Material = SEFI.Material;
        //            if (string.IsNullOrEmpty(SEFI.Material) && TaskAPI.Process != "PKG Sefi Code BOM Application")
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, SEFI.Material, "&nbsp;&nbsp;行" + SEFI.No + ":Code 为必填 "));
        //            }
        //            if (string.IsNullOrEmpty(SEFI.Plant) && TaskAPI.Process != "PKG Sefi Code BOM Application")
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, SEFI.Plant, "&nbsp;&nbsp;行" + SEFI.No + ":Plant 为必填 "));
        //            }
        //            if (string.IsNullOrEmpty(SEFI.Alternative_BOM) && TaskAPI.Process != "PKG Sefi Code BOM Application")
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, SEFI.Alternative_BOM, "&nbsp;&nbsp;行" + SEFI.No + ":Alternative BOM 为必填 "));
        //            }
        //            if (string.IsNullOrEmpty(SEFI.Base_Quantity) && TaskAPI.Process != "PKG Sefi Code BOM Application")
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, SEFI.Base_Quantity, "&nbsp;&nbsp;行" + SEFI.No + ":Base Quantity 为必填 "));
        //            }
        //            if (string.IsNullOrEmpty(SEFI.BOM_Status) && TaskAPI.Process != "PKG Sefi Code BOM Application")
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, SEFI.BOM_Status, "&nbsp;&nbsp;行" + SEFI.No + ":Status 为必填 "));
        //            }
        //        }

        //        foreach (mdc_Bom_Sefi_2 sefi2 in (List<mdc_Bom_Sefi_2>)gv_SEFI_2.DataSource)
        //        {

        //            if (string.IsNullOrEmpty(sefi2.Ict) && string.IsNullOrEmpty(sefi2.Component_Text))
        //            {
        //                continue;
        //            }
        //            if (sefi2.Ict == "L")
        //            //if(!(string.IsNullOrEmpty(sefi2.Ict) && sefi2.Ict=="L"))
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, sefi2.Component_Text, "&nbsp;&nbsp;行" + sefi2.No + ": Component/Text 为必填"));
        //                regs.Add(new Regular(RegularExp.NotEmpty, sefi2.Quantity, "&nbsp;&nbsp;行" + sefi2.No + ": Quantity 为必填"));
        //            }
        //            if (sefi2.Ict == "T")
        //            //if (!(string.IsNullOrEmpty(sefi2.Ict) && sefi2.Ict == "T"))
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, sefi2.Component_Text, "&nbsp;&nbsp;行" + sefi2.No + ": Component/Text 为必填"));
        //            }
        //        }
        //    }




        //    if (Status.Category == "FINI")
        //    {
        //        regs.Add(new Regular(RegularExp.NotEmpty, rd_FINI_1.SelectedValue, "&nbsp;&nbsp;请选择BOM类型"));
        //        foreach (mdc_Bom_Fini_1 FINI in (List<mdc_Bom_Fini_1>)gv_FINI_1.DataSource)
        //        {
        //            if (string.IsNullOrEmpty(FINI.Change_From_Body_Code) && string.IsNullOrEmpty(FINI.Change_From_Lid_Code)
        //                && string.IsNullOrEmpty(FINI.Change_To_Body_Code) && string.IsNullOrEmpty(FINI.Change_To_Lid_Code) && string.IsNullOrEmpty(FINI.Plant))
        //            {
        //                continue;
        //            }

        //            if (string.IsNullOrEmpty(FINI.Change_From_Body_Code) | string.IsNullOrEmpty(FINI.Change_From_Lid_Code)
        //                | string.IsNullOrEmpty(FINI.Change_To_Body_Code) | string.IsNullOrEmpty(FINI.Change_To_Lid_Code) | string.IsNullOrEmpty(FINI.Plant))
        //            {
        //                regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":Code 和 Plant 为必填 "));
        //            }
        //        }
        //        if ((UserAPI.Department.IndexOf("Marketing") >= 0 || UserAPI.Department.IndexOf("Planning") >= 0) && BomActions == "Creation")
        //            foreach (mdc_Bom_Fini_2 FINI in (List<mdc_Bom_Fini_2>)gv_FINI_2.DataSource)
        //            {
        //                if (string.IsNullOrEmpty(FINI.No.ToString()))
        //                {
        //                    continue;
        //                }
        //                //if (string.IsNullOrEmpty(FINI.Plant) && UserAPI.Department.IndexOf("Planning") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":计划部 Plant 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.SKU) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 SKU 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.SEFI_Code) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 SEFI CODE 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Packager) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 包装罐 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Packager_Q) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 包装罐数量 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Carton) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 纸箱 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Carton_Q) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 纸箱数量 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Tagboard) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 标签 为必填 "));
        //                //if (string.IsNullOrEmpty(FINI.Tagboard_Q) && UserAPI.Department.IndexOf("Marketing") >= 0) regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + FINI.No + ":新建时 标签数量 为必填 "));
        //            }
        //        if (gv_FINI_2.Rows.Count <= 0)
        //        {
        //            regs.Add(new Regular(RegularExp.NotEmpty, ((List<mdc_Bom_Fini_2>)gv_FINI_2.DataSource)[0].SKU, "&nbsp;&nbsp;行" + "FINI BOM: 明细行中至少填写 1 行数据"));
        //        }
        //    }

        //    vald_s.AddRange(Validation(regs));

        //    if (vald_s.Count > 0)
        //    {
        //        vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
        //        vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
        //    }

        //    return vald_s;
        //}
        //#endregion

    }
}